<?php
	/*
		This file downloads a given project as a JSON to be saved on the user's computer
	*/
	require_once("../functions.php");
	
	if(!isLoggedIn()){
		echo "Please login first.";
		die();
	}
	
	if(!isset($_GET["id"])){
		die();
	}
	
	$opts = array(
	  'http'=>array(
		'method'=>"GET",
		'header'=>"Accept-language: en\r\n" .
				  "Cookie: id=$_GET[id]\r\n"
	  )
	);

	$host = $_SERVER["SERVER_ADDR"];
	$path = str_replace("download.php","openDataStore.php", $_SERVER["REQUEST_URI"]);
	$url = "http://$host$path";
	
	appendDebug($url);
	$content = file_get_contents($url, FILE_TEXT, stream_context_create($opts));
	
	header("Content-length: ". mb_strlen($content));
	header("Content-type: application/json");
	header("Content-Disposition: attachment; filename=project_".$_GET["id"].".json");
	echo $content;
	exit;
?>